package com.spark.check

import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.spark.colliery.common.ext.gone
import com.spark.colliery.common.ext.isVisible
import com.spark.common.BaseFragment
import com.spark.common.model.BorrowListModel
import com.spark.common.widget.DataView
import kotlinx.android.synthetic.main.frag_duty_dispatch_std_data.*

/**
 * 职责分工 -> 标化资料
 */
class DutyDispatchStdDataFragment : BaseFragment() {
    private lateinit var dataAdapter: DataAdapter

    companion object {
        fun newInstance(): DutyDispatchStdDataFragment {
            val fragment = DutyDispatchStdDataFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_duty_dispatch_std_data
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dataAdapter = DataAdapter(R.layout.item_duty_data)
        recy_datas.adapter = dataAdapter
        recy_datas.layoutManager = LinearLayoutManager(context)

        dataAdapter.addData(mutableListOf(
                BorrowListModel.BorrowItemModel(name = "反国家分裂法", serial = "XCD-0002", regular = "反国家分裂法", created = "2018-02-02", tags = mutableListOf("国家", "自古以来", "明天过后"), state = "剩3天", books = mutableListOf(BorrowListModel.BorrowItemModel.BookModel(name = "反国家分裂法"), BorrowListModel.BorrowItemModel.BookModel(name = "反国家分裂法")))
        ))
    }
}

class DataAdapter(@LayoutRes layoutRes: Int) : BaseQuickAdapter<BorrowListModel.BorrowItemModel, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: BorrowListModel.BorrowItemModel?) {
        helper?.apply {
            if (item == null) return
            setText(R.id.tv_name, item.name)
            setText(R.id.tv_state, item.state)
            setText(R.id.tv_serial, "规范：${item.name}")
            getView<DataView>(R.id.ll_books).bind(item.books)

            val rlInfo = getView<RelativeLayout>(R.id.rl_info)
            rlInfo.gone(true)
            getView<ImageView>(R.id.iv_expand).setOnClickListener {
                val state = rlInfo.isVisible()
                rlInfo.gone(state)
            }
        }
    }

}